<?php
/**
 * autoUser_notice actions.
 *
 * @package    ##PROJECT_NAME##
 * @subpackage autoUser_notice
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: actions.class.php 9855 2008-06-25 11:26:01Z FabianLange $
 */
class user_noticeActions extends sfActions
{
  public function executeIndex()
  {
    return $this->forward('user_notice', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/user_notice/filters');

    // pager
    $this->pager = new sfPropelPager('UserNotice', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/user_notice')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/user_notice');
    }
  }

  public function executeCreate()
  {
    return $this->forward('user_notice', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('user_notice', 'edit');
  }

  public function executeEdit()
  {
    $this->user_notice = $this->getUserNoticeOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateUserNoticeFromRequest();

      $this->saveUserNotice($this->user_notice);

      $this->setFlash('notice', 'Thông tin đã được cập nhật');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('user_notice/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('user_notice/list');
      }
      else
      {
        return $this->redirect('user_notice/edit?id='.$this->user_notice->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->user_notice = UserNoticePeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->user_notice);

    try
    {
      $this->deleteUserNotice($this->user_notice);
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected User notice. Make sure it does not have any associated items.');
      return $this->forward('user_notice', 'list');
    }

    return $this->redirect('user_notice/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->user_notice = $this->getUserNoticeOrCreate();
    $this->updateUserNoticeFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function saveUserNotice($user_notice)
  {
    $user_notice->save();

  }

  protected function deleteUserNotice($user_notice)
  {
    $user_notice->delete();
  }

  protected function updateUserNoticeFromRequest()
  {
    $user_notice = $this->getRequestParameter('user_notice');

    if (isset($user_notice['title']))
    {
      $this->user_notice->setTitle($user_notice['title']);
    }
    if (isset($user_notice['content']))
    {
      $this->user_notice->setContent($user_notice['content']);
    }
    if (isset($user_notice['created_datetime']))
    {
      if ($user_notice['created_datetime'])
      {
        try
        {
          $dateFormat = new sfDateFormat($this->getUser()->getCulture());
                              if (!is_array($user_notice['created_datetime']))
          {
            $value = $dateFormat->format($user_notice['created_datetime'], 'I', $dateFormat->getInputPattern('g'));
          }
          else
          {
            $value_array = $user_notice['created_datetime'];
            $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
          }
          $this->user_notice->setCreatedDatetime($value);
        }
        catch (sfException $e)
        {
          // not a date
        }
      }
      else
      {
        $this->user_notice->setCreatedDatetime(null);
      }
    }
    if (isset($user_notice['to_user']))
    {
      $this->user_notice->setToUser($user_notice['to_user']);
    }
    if (isset($user_notice['notice_type']))
    {
      $this->user_notice->setNoticeType($user_notice['notice_type']);
    }
    if (isset($user_notice['notice_order']))
    {
      $this->user_notice->setNoticeOrder($user_notice['notice_order']);
    }
  }

  protected function getUserNoticeOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $user_notice = new UserNotice();
    }
    else
    {
      $user_notice = UserNoticePeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($user_notice);
    }

    return $user_notice;
  }

  protected function processFilters()
  {
    if ($this->getRequest()->hasParameter('filter'))
    {
      $filters = $this->getRequestParameter('filters');
      if (isset($filters['created_datetime']['from']) && $filters['created_datetime']['from'] !== '')
      {
        $filters['created_datetime']['from'] = sfI18N::getTimestampForCulture($filters['created_datetime']['from'], $this->getUser()->getCulture());
      }
      if (isset($filters['created_datetime']['to']) && $filters['created_datetime']['to'] !== '')
      {
        $filters['created_datetime']['to'] = sfI18N::getTimestampForCulture($filters['created_datetime']['to'], $this->getUser()->getCulture());
      }

      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/user_notice');
      $this->getUser()->getAttributeHolder()->removeNamespace('sf_admin/user_notice/filters');
      $this->getUser()->getAttributeHolder()->add($filters, 'sf_admin/user_notice/filters');
    }
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/user_notice/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/user_notice/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/user_notice/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['id_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::ID, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::ID, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['id']) && $this->filters['id'] !== '')
    {
      $c->add(UserNoticePeer::ID, $this->filters['id']);
    }
    if (isset($this->filters['title_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::TITLE, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::TITLE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['title']) && $this->filters['title'] !== '')
    {
      $c->add(UserNoticePeer::TITLE, strtr($this->filters['title'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['content_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::CONTENT, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::CONTENT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['content']) && $this->filters['content'] !== '')
    {
      $c->add(UserNoticePeer::CONTENT, strtr($this->filters['content'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['created_datetime_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::CREATED_DATETIME, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::CREATED_DATETIME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['created_datetime']))
    {
      if (isset($this->filters['created_datetime']['from']) && $this->filters['created_datetime']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(UserNoticePeer::CREATED_DATETIME, $this->filters['created_datetime']['from'], Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['created_datetime']['to']) && $this->filters['created_datetime']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(UserNoticePeer::CREATED_DATETIME, $this->filters['created_datetime']['to'], Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(UserNoticePeer::CREATED_DATETIME, $this->filters['created_datetime']['to'], Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
    if (isset($this->filters['to_user_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::TO_USER, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::TO_USER, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['to_user']) && $this->filters['to_user'] !== '')
    {
      $c->add(UserNoticePeer::TO_USER, $this->filters['to_user']);
    }
    if (isset($this->filters['notice_type_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::NOTICE_TYPE, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::NOTICE_TYPE, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['notice_type']) && $this->filters['notice_type'] !== '')
    {
      $c->add(UserNoticePeer::NOTICE_TYPE, strtr($this->filters['notice_type'], '*', '%'), Criteria::LIKE);
    }
    if (isset($this->filters['notice_order_is_empty']))
    {
      $criterion = $c->getNewCriterion(UserNoticePeer::NOTICE_ORDER, '');
      $criterion->addOr($c->getNewCriterion(UserNoticePeer::NOTICE_ORDER, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['notice_order']) && $this->filters['notice_order'] !== '')
    {
      $c->add(UserNoticePeer::NOTICE_ORDER, $this->filters['notice_order']);
    }
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/user_notice/sort'))
    {
      $sort_column = UserNoticePeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/user_notice/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'user_notice{id}' => 'ID:',
      'user_notice{title}' => 'Tiêu đề:',
      'user_notice{content}' => 'Nội dung:',
      'user_notice{created_datetime}' => 'Thời gian:',
      'user_notice{to_user}' => 'Gửi đến:',
      'user_notice{notice_type}' => 'Kiểu:',
      'user_notice{notice_order}' => 'Thứ tự:',
    );
  }
}